const router = require('koa-router')()
const db = require('../db/initdb')

router.prefix("/back/feedback")

router.get("/", async (ctx, next) => {
    // console.log("feedback")

    // id，用户id，用户昵称，用户头像，反馈内容，联系方式，时间，是否回复，用户是否查看回复
    let sql = `select 
            hs.return.id as key,
            userid,
            name,
            avatar,
            content,
            tele,
            date,
            resolve,
            recontent,
            view
            from hs.return, hs.user
            where hs.return.userid = hs.user.id
            order by resolve, view, date`
    let result = await db.query(sql)
    console.log(result.rows)

    ctx.body = result.rows
})

router.post("/reply", async (ctx, next) => {
    // console.log(ctx.request.body)
    let id = ctx.request.body.id
    let recontent = ctx.request.body.recontent

    let sql = 'update hs.return set recontent = $1, resolve = true where id = $2'
    let result = await db.query(sql, [recontent, id])
    // console.log(result)
    if (result.rowCount == 1) {
        ctx.body = 'ok'
    }
})



module.exports = router
